package com.leonzhangxf.vdt.manipulation;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * A demonstrate for bean manipulation.
 *
 * @author leonzhangxf
 * @date 20190423
 */
@Configuration
@ComponentScan({"com.leonzhangxf.vdt.manipulation"})
public class ApplicationForBeanManipulationStarter {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
            new AnnotationConfigApplicationContext(ApplicationForBeanManipulationStarter.class);

        demoBeanWrapper();
    }

    private static void demoBeanWrapper() {
        BeanWrapper employee = new BeanWrapperImpl(new Employee());
        employee.setPropertyValue("name", "leonzhangxf");

        PropertyValue age = new PropertyValue("age", 24);
        employee.setPropertyValue(age);

        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("email", 100);
        propertyValues.add("lol", "lol");
        propertyValues.addPropertyValue(age);
        employee.setPropertyValues(propertyValues, true, true);

        BeanWrapper address = new BeanWrapperImpl(new Address());
        address.setPropertyValue("country", "China");
        address.setPropertyValue("province", "Beijing");
        address.setPropertyValue("city", "Beijing");

        employee.setPropertyValue("address", address.getWrappedInstance());

        System.out.println(address.getWrappedInstance());
        System.out.println(employee.getWrappedInstance());
    }

}
